package com.lanou.dllo.baisibudejie.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.dllo.baisibudejie.constant.Constant;
import com.lanou.dllo.baisibudejie.db.entity.User;

/**
 * Created by dllo on 15/10/22.
 */
public class MySQLiteLoginUtil {
    private SQLiteDatabase db;
    private Context context;

    public MySQLiteLoginUtil(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
        init();
    }

    private void init() {
        MySQLliteOpenHelper helper = new MySQLliteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        db = helper.getWritableDatabase();
    }

    public void saveDatasToDataBase(User user) {
            ContentValues values = new ContentValues();
            values.put(Constant.COLUMN_USER_NAME, user.getName());
            values.put(Constant.COLUMN_USER_PASSWORD, user.getPassword());
            db.insert(Constant.TABLE_NAME_USER, null, values);
    }

    public String queryByLoginName(String name) {
        String sql = " SELECT * FROM " + Constant.TABLE_NAME_USER + " WHERE name = " + name;
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor != null) {
            String password = null;
            while (cursor.moveToNext()) {
                password = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_USER_PASSWORD));
            }
            cursor.close();
            return password;
        }
        return null;
    }




}
